package com.project.fortuneteller.controller;


import com.project.fortuneteller.context.UserContext;
import com.project.fortuneteller.pojo.UserInfo;
import com.project.fortuneteller.pojo.dto.HandleNewFriendDto;
import com.project.fortuneteller.pojo.vo.AddNewFriendDto;
import com.project.fortuneteller.service.impl.FriendServiceImpl;
import com.project.fortuneteller.service.impl.UserServiceImpl;
import com.project.fortuneteller.utils.Result;
import com.project.fortuneteller.utils.ThreeCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 好友的添加操作
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendServiceImpl friendService;

    @Autowired
   private UserServiceImpl userService;

    /**
     *  查找用户所有好友方法
     * @return
     */
    @RequestMapping("/getAllFriend")
    public Result findFriends(){
        // 获取token 的值
        UserInfo userInfo = UserContext.getUserInfo();
        String tokenVal = userInfo.getToken();

        log.info("FriendController中获取 token: {}", tokenVal);
        Integer value = null;
        if(tokenVal != null) {
            value = (Integer) ThreeCache.get(tokenVal);
        }
        if (value == null) {
            return Result.error("你未登录,请先登录");
        }

        return  friendService.findFriends(value);
    }

    /**
     * 搜索新的 未添加的用户
     * @param nickStr
     * @return
     */
    @RequestMapping("/findUser")
    public Result findNewFriend(String nickStr){
        // 获取token 的值
        String tokenVal = UserContext.getUserInfo().getToken();
        // 获取
        return userService.getLikeByUserNickName(tokenVal,nickStr);
    }

    /**
     *  同意用户好友添加
     */
    @RequestMapping("/handleAdd")
    public Result handleAddFriend(   @RequestBody HandleNewFriendDto handleNewFriendDto){
        return friendService.handleAddFriend(Integer.parseInt(UserContext.getUserId()),handleNewFriendDto);
    }

    /**
     *  添加用户新的好友的方法
     */
    @RequestMapping("/add")
    public Result addNewFriend(@RequestBody AddNewFriendDto addNewFriendDto){
        return friendService.addNewFriend(Integer.parseInt(UserContext.getUserId()),addNewFriendDto.getFriendId());
    }

    /**
     * 获取 待同意的好友申请
     * @return
     */
    @RequestMapping("/getNewFriendInfo")
    public Result getNewFriendInfo(){
        return friendService.getNewFriendInfo(Integer.parseInt(UserContext.getUserId()));
    }

}
